package com.googlecode.lemyriapode.type;

import com.googlecode.lemyriapode.regle.Position;
import com.googlecode.lemyriapode.regle.Position.Piece;


import com.googlecode.lemyriapode.type.Echiquier.Case;
import com.googlecode.lemyriapode.type.Echiquier.Direction;
import com.googlecode.lemyriapode.type.PredClosFactory.PredClosType;

public abstract class AFPosition extends AChessCat<Object, Object> {

    public IPredicate CaseInexistente;
    public IPredicate PieceAJouer,  FinDirection,  DirectionAutorise,  DeplacementNCase;
    public IClosure explorerDirection,  RienAFaire,  exploreDir,  exploreDir1Case;
    public Case _c;
    public Piece piece_c;
    public Direction dir;
    public Echiquier echq;
//    public Run analyse;
    public Position position;
    public Position pos;
    public int I;
    public int J;
    public boolean flag1Case;
    private Direction direction;
    private Echiquier echiquier;

    public AFPosition() {

//      FinDirection =  PredClosFactory.create(PredClosType.FinDirection);


        FinDirection = new IPredicate() {

            public boolean eval() {

                echiquier = pos.getEchiquier();
                direction = echiquier.getDirection();
                return !(direction.ordinal() > 7);
            }
        };
        CaseInexistente = new IPredicate() {

            public boolean eval() {
                return !pos.getEchiquier().getCase().equals(Case.out);


            }
        };
        PieceAJouer = new IPredicate() {

            public boolean eval() {
                return (piece_c != null) && (piece_c.isCouleur() && pos.isTrait());
            }
        };

        RienAFaire = new IClosure() {

            public void exec() {
            }
        };
        DirectionAutorise = new IPredicate() {

            public boolean eval() {
                return dir.estInclus(piece_c.getRegle_deplacement());
            }
        };

        DeplacementNCase = new IPredicate() {

            public boolean eval() {
                return piece_c.isNCase();
            }
        };
    }

    @Override
    public abstract boolean continuer(int n, Object e);

    @Override
    public abstract void executer(int n, Object e);

    @Override
    public abstract Object stopper(int n, Object e);

    public static void SiSinon(IPredicate pred, IClosure trueclos, IClosure falseclos) {
        if (pred.eval()) {
            trueclos.exec();
        } else {
            falseclos.exec();
        }
    }

    public void _Case_Suivante() {
        echq.setCase(_c.next());
    }

    public void _Piece_En_C() {
        piece_c = position.getPositionCourante().get(_c);
    }

    public void _Case_C() {

        echq = pos.getEchiquier();
        _c = echq.getCase();
    }

    public void getNext_dir() {
        echq.setDirection(dir.next());
    }

    public void getDir() {

        dir = pos.getEchiquier().getDirection();
    }
}
